class Event < ActiveRecord::Base
  validates_presence_of :name,:message =>"您好像没有给会议指定名称?"
  validates_numericality_of :days,:message=>'大意了吧，会议天数是不是应该是数字呢？'

  has_many :conventioners,:dependent=>:destroy
  has_many :users,:through=>:conventioners,:source=>:user
  has_many :arrife,:class_name=>"Arrive",:dependent=>:destroy
  has_many :reverts,:class_name=>"Revert",:dependent=>:destroy
  has_many :rooms,:dependent=>:destroy
  has_many :custom_work_items,:class_name=>"Custom_WorkItem",:dependent=>:destroy
  has_many :must_work_items,:class_name=>"Must_WorkItem",:dependent=>:destroy
  has_many :dines,:dependent=>:destroy
  has_many :meeting_places,:dependent=>:destroy
  has_many :group_photos,:dependent=>:destroy
  WorkOption = Struct.new(:id,:name)


  def add_must_work_items
    add_tasks_to(add_must_work_item('组织会议资源','如人员、房间等信息的添加'))
  end

  #参会单位
  def froms
    self.users.find(:all,:select=>'form',:group=>'form').collect{|group| group.form}.join(',')
  end

  #就餐地点
  def dine_locations
    self.dines.find(:all,:select=>'location',:group=>'location').collect{|dine| dine.location}.join(',')
  end

  #房间类型与套数
  def room_groups
    self.rooms.find(:all,:select=>'room_category as category,count(room_category) as count',:group=>'room_category')
  end

  #用户的类型种类
  def user_categorys
    user_categorys={}
    self.conventioners.find(:all,:select=>'category,count(category) as count',:group=>'category').each do |conventioner|
      user_categorys[conventioner.category] = conventioner.count
    end
    user_categorys
  end

  def get_users_by_from(from)
    self.users.find_all_by_form(from).collect { |user| user.conventioners.find_by_event_id(self.id)  }
  end

  def get_users_by_level(level)
    self.users.find_all_by_level(level).collect{|user| user.conventioners.find_by_event_id(self.id)}
  end

  #工作人员
  def workers
    workers=[]
    conventioners=self.conventioners.find(:all,:select=>'id,user_id',:conditions=>"category='工作人员'")
    conventioners.each do |conventioner|
      workers << WorkOption.new(conventioner.id,conventioner.user.name)
    end
    workers
  end

  #与会领导
  def leaders
    self.conventioners.find_all_by_is_leader(true)
  end

  #已入住的人
  def checked_in
    self.conventioners.find(:all,:conditions=>"room_id > 0")
  end

  #未入住的人
  def unchecked_in
    self.conventioners - checked_in
  end

private
  
  def add_must_work_item(name,description)
    work_item=Must_WorkItem.new()
    work_item.name=name
    work_item.description=description
    work_item.start_date=self.occurs_on
    work_item.event=self
    work_item.save
    work_item
  end
  def add_tasks_to(work_item)
    add_task(work_item,"组织与会人员","user")
    add_task(work_item,"安排行程","trip")
    add_task(work_item,"安排房间","room")
    add_task(work_item,"安排宴请","dine")
    add_task(work_item,"自定义工作项","workitem")
  end
  def add_task(work_item,name,link=nil)
    task = Task.new
    task.work_item = work_item
    task.completed=0
    task.name = name
    task.link = link
    task.save
  end
end
